import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  HttpStatus,
  Query,
  ParseIntPipe,
  Req,
} from '@nestjs/common';
import { UserVisitService } from './user-visit.service';
import { CreateUserVisitDto } from './dto/create-user-visit.dto';
import {
  ApiBody,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';

@ApiTags('用户访问记录')
@Controller('user-visit')
export class UserVisitController {
  constructor(private readonly userVisitService: UserVisitService) {}

  // 通过 @ApiOperation 描述接口的信息，@ApiResponse 描述返回值信息，@ApiQuery 描述 query 参数信息。
  @ApiOperation({ summary: '新增用户访问记录', description: '新增用户访问记录接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '用户访问记录成功',
    type: CreateUserVisitDto,
  })
  @ApiBody({
    type: CreateUserVisitDto,
  })
  @Post()
  create(@Body() createUserVisitDto: CreateUserVisitDto, @Req() req: any) {
    console.log('req:==>', req);
    return this.userVisitService.create(createUserVisitDto);
  }

  @ApiOperation({ summary: '查询用户访问记录列表' })
  @ApiQuery({
    name: 'page',
    type: Number,
    description: '当前页码',
    required: true,
    example: 1,
  })
  @ApiQuery({
    name: 'pageSize',
    type: Number,
    description: '分页大小',
    required: true,
    example: 10,
  })
  @ApiQuery({
    name: 'region',
    type: String,
    description: '地区',
    required: false,
    example: '广东省广州市',
  })
  @Get('page')
  findAll(
    @Query('page', new ParseIntPipe()) page = 1,
    @Query('pageSize', new ParseIntPipe()) pageSize = 10,
    @Query('region') region?: string,
  ) {
    return this.userVisitService.pageSearch({
      page,
      pageSize,
      region,
    });
  }

  @ApiOperation({ summary: '删除用户访问记录' })
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.userVisitService.remove(id);
  }
}
